//
//  WeiboViewLayoutFrame.m
//  Custom_Weibo
//
//  Created by LEO on 12/11/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "WeiboViewLayoutFrame.h"

@implementation WeiboViewLayoutFrame

#pragma mark - INITIALIZATION_METHODS
- (void)setWeiboModel:(WeiboModel *)weiboModel {
    if (_weiboModel != weiboModel) {
        _weiboModel = weiboModel;
        [self redesignFrame];
    }
}

//  Each widget should be redesigned their frame.
- (void)redesignFrame {
    /*  There are table view in comment list and home view, we should distinguish
     them from each other
     */
    CGFloat weiboViewWidth = 0;
    
    CGFloat weiboFontSize = kFontSize_Weibo(_isDetail);
    CGFloat repostWeiboFontSize = kFontSize_ReWeibo(_isDetail);
    
    /* 1)Whole weibo view frame.If it is comment view,then the side should be
     close to the screen side,or it just lay under the user image view.
     */
    if (_isDetail) {
        weiboViewWidth = kScreenWidth;
        _mainFrame = CGRectMake(0, 0, weiboViewWidth, 0);
    }else {
        weiboViewWidth = kScreenWidth - 90;
        _mainFrame = CGRectMake(70, 40, weiboViewWidth, 0);
    }
    
    // 2)Original weibo frame.
    NSString *text = _weiboModel.text;
    
    CGFloat textWidth = weiboViewWidth - kWeiboSideMultiple * kWeiboSideBlank;
    CGFloat textHeight = [WXLabel getTextHeight:weiboFontSize
                                          width:textWidth
                                           text:text
                                      linespace:5.0f];
    _textFrame = CGRectMake(kWeiboSideBlank, kWeiboSideBlank, textWidth, textHeight);
    
    /* 3)Make a judgement about whether it is repost weibo and has a 
     picture or not.
     */
    if (_weiboModel.repostWeiboModel == nil) {
        NSString *thumbImage = _weiboModel.thumbnailImage;
        if (thumbImage != nil) {
            //  Set the image size as follows.
            CGFloat imageX = CGRectGetMinX(_textFrame);
            CGFloat imageY = CGRectGetMaxY(_textFrame) + kWeiboSideBlank;
            _imageFrame = CGRectMake(imageX, imageY,
                                     kThumbImageWidth, kThumbImageHeight);
        }
    }else {
        NSString *repostText = _weiboModel.repostWeiboModel.text;
    
        CGFloat repostTextWidth = textWidth - kWeiboSideMultiple * kWeiboSideBlank;
        CGFloat repostTextHeight = [WXLabel getTextHeight:repostWeiboFontSize
                                                    width:repostTextWidth
                                                     text:repostText
                                                linespace:5.0f];
        
        CGFloat repostTextX = CGRectGetMinX(_textFrame) + kWeiboSideBlank;
        CGFloat repostTextY = CGRectGetMaxY(_textFrame) + kWeiboSideBlank;
        
        _repostTextFrame = CGRectMake(repostTextX, repostTextY,
                                      repostTextWidth, repostTextHeight);
        
        NSString *repostThumbImage = _weiboModel.repostWeiboModel.thumbnailImage;
        if (repostThumbImage != nil) {
            CGFloat repostImageX = CGRectGetMinX(_repostTextFrame);
            CGFloat repostImageY = CGRectGetMaxY(_repostTextFrame) + kWeiboSideBlank;
            _imageFrame = CGRectMake(repostImageX, repostImageY,
                                     kThumbImageWidth, kThumbImageHeight);
        }
        
        CGFloat bgX = CGRectGetMinX(_textFrame);
        CGFloat bgY = CGRectGetMaxY(_textFrame);
        CGFloat bgWidth = textWidth;
        CGFloat bgHeight = 0;
        
        if (repostThumbImage != nil) {
            bgHeight = CGRectGetMaxY(_imageFrame) - bgY + kWeiboSideBlank;
        }else {
            bgHeight = CGRectGetMaxY(_repostTextFrame) - bgY + kWeiboSideBlank;
        }
        _backgroundImageFrame = CGRectMake(bgX, bgY, bgWidth, bgHeight);
    }
    
    CGRect mainFrame = _mainFrame;
    if (_weiboModel.repostWeiboModel != nil) {
        mainFrame.size.height = CGRectGetMaxY(_backgroundImageFrame);
    } else if (_weiboModel.thumbnailImage != nil) {
        mainFrame.size.height = CGRectGetMaxY(_imageFrame);
    } else {
        mainFrame.size.height = CGRectGetMaxY(_textFrame);
    }
    _mainFrame = mainFrame;
}

@end
